package com.lill.product.web;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.lill.common.component.util.PageUtils;
import com.lill.common.component.util.Query;
import com.lill.common.component.util.R;
import com.lill.product.model.ProductBoutiqueModel;
import com.lill.product.service.IProductBoutiqueService;
import com.lill.support.annotation.CurrentUser;
import com.lill.system.constant.SystemConstant;
import com.lill.system.model.SysUserEntity;

/**
 * 精品信息Controller
 *
 * @author lill
 * @date 2021-08-27 09:14:43
 */
@RestController
@RequestMapping("product/boutique")
public class ProductBoutiqueController {
    @Autowired
    private IProductBoutiqueService productBoutiqueService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params,@CurrentUser SysUserEntity user) {
    	if(!user.getId().equals(SystemConstant.SUPER_ADMIN)){
    		params.put("companyId", user.getCompanyId());
    	}
        //查询列表数据
        Query query = new Query(params);

        List<ProductBoutiqueModel> productBoutiqueList = productBoutiqueService.queryList(query);
        int total = productBoutiqueService.queryTotal(query);
        PageUtils pageUtil = new PageUtils(productBoutiqueList, total, query.getLimit(), query.getPage());
        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") String id) {
    	ProductBoutiqueModel productBoutique = productBoutiqueService.queryObject(id);
        return R.ok().put("productBoutique", productBoutique);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody ProductBoutiqueModel productBoutique,@CurrentUser SysUserEntity user) {
    	productBoutique.setTopClassifyId(ProductBoutiqueModel.classify);
    	productBoutique.setCompanyId(user.getCompanyId());
    	productBoutique.setStatus(1);
    	productBoutique.setState(0);
    	productBoutique.setCreateUser(user.getId());
    	productBoutique.setCreateTime(new Date());
    	productBoutique.setAllName(joinAllname(productBoutique));
        int result=productBoutiqueService.save(productBoutique);
		if(result>0){
        	return R.ok();
        }else{
        	return R.failure();
        }
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody ProductBoutiqueModel productBoutique,@CurrentUser SysUserEntity user) {
    	if(ProductBoutiqueModel.classify.equals(productBoutique.getId())){
    		return R.failure().put("msg", "系统权限不足");
    	}
    	productBoutique.setTopClassifyId(ProductBoutiqueModel.classify);
    	productBoutique.setCompanyId(user.getCompanyId());
    	productBoutique.setUpdateUser(user.getId());
    	productBoutique.setUpdateTime(new Date());
    	productBoutique.setAllName(joinAllname(productBoutique));
        int result=productBoutiqueService.update(productBoutique);
		if(result>0){
        	return R.ok();
        }else{
        	return R.failure();
        }
    }
    
    @RequestMapping("/sale")
    public R sale(@RequestBody String[] ids,@CurrentUser SysUserEntity user) {
    	Map<String, Object> params=new HashMap<String, Object>();
    	params.put("ids", ids);
    	params.put("updateUser", user.getId());
    	params.put("updateTime", new Date());
    	params.put("state", 1);
    	int result=productBoutiqueService.handleBoutique(params);
    	if(result>0){
    		return R.ok();
    	}else{
    		return R.failure();
    	}
    }
    @RequestMapping("/stop")
    public R stop(@RequestBody String[] ids,@CurrentUser SysUserEntity user) {
    	Map<String, Object> params=new HashMap<String, Object>();
    	params.put("ids", ids);
    	params.put("updateUser", user.getId());
    	params.put("updateTime", new Date());
    	params.put("state", 2);
    	int result=productBoutiqueService.handleBoutique(params);
    	if(result>0){
    		return R.ok();
    	}else{
    		return R.failure();
    	}
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    public R queryAll(@RequestParam Map<String, Object> params,@CurrentUser SysUserEntity user) {
    	if(!user.getId().equals(SystemConstant.SUPER_ADMIN)){
    		params.put("companyId", user.getCompanyId());
    	}
        List<ProductBoutiqueModel> list = productBoutiqueService.queryList(params);
        return R.ok().put("list", list);
    }
    
    private String joinAllname(ProductBoutiqueModel productBoutique){
        StringBuffer allNameBuffer=new StringBuffer("");
        if(StringUtils.isNotEmpty(productBoutique.getName())){
        	if(allNameBuffer.length()>0){
        		allNameBuffer.append(" ");
        	}
        	allNameBuffer.append(productBoutique.getName());
        }
        if(StringUtils.isNotEmpty(productBoutique.getBrand())){
        	if(allNameBuffer.length()>0){
        		allNameBuffer.append(" ");
        	}
        	allNameBuffer.append(productBoutique.getBrand());
        }
        if(StringUtils.isNotEmpty(productBoutique.getNewType())){
        	if(allNameBuffer.length()>0){
        		allNameBuffer.append(" ");
        	}
        	allNameBuffer.append(productBoutique.getNewType());
        }
        
        if(StringUtils.isNotEmpty(productBoutique.getUnit())){
        	if(allNameBuffer.length()>0){
        		allNameBuffer.append(" ");
        	}
        	allNameBuffer.append(productBoutique.getUnit());
        }
        return allNameBuffer.toString();
    }
}
